package com.itheima.admin.controller.v1;

import com.itheima.admin.service.AdminManageService;
import com.itheima.model.admin.dtos.AdminCreateDto;
import com.itheima.model.admin.dtos.AdminQueryDto;
import com.itheima.model.admin.dtos.AdminUpdateDto;
import com.itheima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理员管理控制器
 * 
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/admin")
@Api(tags = "管理员管理")
public class AdminManageController {

    @Autowired
    private AdminManageService adminManageService;

    /**
     * 分页查询管理员列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询管理员列表")
    public ResponseResult listAdmins(@RequestBody AdminQueryDto dto) {
        return adminManageService.listAdmins(dto);
    }

    /**
     * 根据ID查询管理员详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询管理员详情")
    public ResponseResult getAdminById(@PathVariable Integer id) {
        return adminManageService.getAdminById(id);
    }

    /**
     * 创建管理员
     */
    @PostMapping
    @ApiOperation("创建管理员")
    public ResponseResult createAdmin(@RequestBody AdminCreateDto dto) {
        return adminManageService.createAdmin(dto);
    }

    /**
     * 更新管理员信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新管理员信息")
    public ResponseResult updateAdmin(@PathVariable Integer id, @RequestBody AdminUpdateDto dto) {
        return adminManageService.updateAdmin(id, dto);
    }

    /**
     * 删除管理员
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除管理员")
    public ResponseResult deleteAdmin(@PathVariable Integer id) {
        return adminManageService.deleteAdmin(id);
    }

    /**
     * 启用/禁用管理员
     */
    @PutMapping("/{id}/status")
    @ApiOperation("启用/禁用管理员")
    public ResponseResult toggleAdminStatus(@PathVariable Integer id, @RequestBody Map<String, Integer> params) {
        Integer status = params.get("status");
        return adminManageService.toggleAdminStatus(id, status);
    }

    /**
     * 重置管理员密码
     */
    @PutMapping("/{id}/password")
    @ApiOperation("重置管理员密码")
    public ResponseResult resetPassword(@PathVariable Integer id, @RequestBody Map<String, String> params) {
        String newPassword = params.get("newPassword");
        return adminManageService.resetPassword(id, newPassword);
    }

    /**
     * 分配角色给管理员
     */
    @PutMapping("/{id}/roles")
    @ApiOperation("分配角色给管理员")
    public ResponseResult assignRoles(@PathVariable Integer id, @RequestBody Map<String, List<Integer>> params) {
        List<Integer> roleIds = params.get("roleIds");
        return adminManageService.assignRoles(id, roleIds);
    }
}

